package com.springboot.rigger.modules.controller;

import org.apache.poi.xwpf.usermodel.TOC;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFStyle;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDocument1;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtBlock;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

/**
 * @Description:
 * @title: WordController
 * @Author Star_Chen
 * @Date: 2024/9/5 15:27
 * @Version 1.0
 */
public class WordController {


    public static void main(String[] args) throws IOException {
        try (FileInputStream fis = new FileInputStream("C:\\Users\\Administrator\\Desktop\\word.docx")) {

            XWPFDocument document = new XWPFDocument(fis);

            for (XWPFParagraph paragraph : document.getParagraphs()) {

                // 获取书签位置
//                List<XWPFRun> runs = paragraph.getRuns();
//                for (XWPFRun run : runs) {
//                    run.getCTR().get
//                }


                String styleId = paragraph.getStyle();
                System.out.println(styleId);

                if (styleId != null) {
                    XWPFStyle style = document.getStyles().getStyle(styleId);
                    System.out.println(style.getName());
//                    System.out.println(style.getName());
                    if (style.getName().contains("heading 1")) {
//                        System.out.println(style.getName());
                        System.out.println(paragraph.getText());
                    }
                    if (style.getName().contains("heading 2")) {
                        System.out.println(paragraph.getText());

                    }
                }
//                if ("标题 1".equals(style)){
//                    System.out.println(paragraph.getText());
//                }


            }
            createTOC(document, document.getDocument(), document.getParagraphs());


//            // 保存文档
            try (FileOutputStream out = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\word1.docx")) {
                document.write(out);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    public static void createTOC(XWPFDocument xwpfDocument, CTDocument1 ctDocument1, List<XWPFParagraph> paragraphs) {
        CTSdtBlock block = ctDocument1.getBody().addNewSdt();
        TOC toc = new TOC(block);
        for (XWPFParagraph par : paragraphs) {
            String parStyle = par.getStyle();
            XWPFStyle style = xwpfDocument.getStyles().getStyle(parStyle);
            if (style != null) {
                if (style.getName() != null && style.getName().startsWith("heading")) {
                    try {
                        int level = Integer.parseInt(style.getName().substring("heading".length()+1));
                        System.out.println(style.getName().substring("heading".length()));
                        toc.addRow(level, par.getText(), 1, "112723803");
                    } catch (NumberFormatException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

    }

    @RequestMapping("/doi")
    public void DIOController(){

    }
}
